!pip install yfinance==0.2.4
#!pip install pandas==1.3.3
Requirement already satisfied: yfinance==0.2.4 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (0.2.4)
Requirement already satisfied: cryptography>=3.3.2 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (38.0.2)
Requirement already satisfied: pytz>=2022.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (2022.6)
Requirement already satisfied: appdirs>=1.4.4 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (1.4.4)
Requirement already satisfied: html5lib>=1.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (1.1)
Requirement already satisfied: frozendict>=2.3.4 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (2.3.7)
Requirement already satisfied: multitasking>=0.0.7 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (0.0.11)
Collecting lxml>=4.9.1
Downloading lxml-4.9.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (6.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.6/6.6 MB 66.5 MB/s eta 0:00:00:00:0100:01
Requirement already satisfied: numpy>=1.16.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (1.21.6)
Requirement already satisfied: pandas>=1.3.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (1.3.5)
Requirement already satisfied: requests>=2.26 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.2.4) (2.28.1)
Collecting beautifulsoup4>=4.11.1
Downloading beautifulsoup4-4.12.2-py3-none-any.whl (142 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 143.0/143.0 kB 22.1 MB/s eta 0:00:00
Requirement already satisfied: soupsieve>1.2 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from beautifulsoup4>=4.11.1->yfinance==0.2.4) (2.3.2.post1)
Requirement already satisfied: cffi>=1.12 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from cryptography>=3.3.2->yfinance==0.2.4) (1.15.1)
Requirement already satisfied: webencodings in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from html5lib>=1.1->yfinance==0.2.4) (0.5.1)
Requirement already satisfied: six>=1.9 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from html5lib>=1.1->yfinance==0.2.4) (1.16.0)
Requirement already satisfied: python-dateutil>=2.7.3 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from pandas>=1.3.0->yfinance==0.2.4) (2.8.2)
Requirement already satisfied: charset-normalizer<3,>=2 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.26->yfinance==0.2.4) (2.1.1)
Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.26->yfinance==0.2.4) (2022.12.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.26->yfinance==0.2.4) (1.26.13)
Requirement already satisfied: idna<4,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.26->yfinance==0.2.4) (3.4)
Requirement already satisfied: pycparser in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from cffi>=1.12->cryptography>=3.3.2->yfinance==0.2.4) (2.21)
Installing collected packages: lxml, beautifulsoup4
Attempting uninstall: lxml
Found existing installation: lxml 4.6.4
Uninstalling lxml-4.6.4:
Successfully uninstalled lxml-4.6.4
Attempting uninstall: beautifulsoup4
Found existing installation: beautifulsoup4 4.10.0
Uninstalling beautifulsoup4-4.10.0:
Successfully uninstalled beautifulsoup4-4.10.0
Successfully installed beautifulsoup4-4.12.2 lxml-4.9.2
import yfinance as yf
import pandas as pd
tesla = yf.Ticker("TSLA")
tesla_data = tesla.history(period="max")
tesla_data.reset_index(inplace=True)
tesla_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 00:00:00-04:00 | 1.266667 | 1.666667 | 1.169333 | 1.592667 | 281494500 | 0.0 | 0.0 |
| 1 | 2010-06-30 00:00:00-04:00 | 1.719333 | 2.028000 | 1.553333 | 1.588667 | 257806500 | 0.0 | 0.0 |
| 2 | 2010-07-01 00:00:00-04:00 | 1.666667 | 1.728000 | 1.351333 | 1.464000 | 123282000 | 0.0 | 0.0 |
| 3 | 2010-07-02 00:00:00-04:00 | 1.533333 | 1.540000 | 1.247333 | 1.280000 | 77097000 | 0.0 | 0.0 |
| 4 | 2010-07-06 00:00:00-04:00 | 1.333333 | 1.333333 | 1.055333 | 1.074000 | 103003500 | 0.0 | 0.0 |
tesla_data.tail()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 3216 | 2023-04-10 00:00:00-04:00 | 179.940002 | 185.100006 | 176.110001 | 184.509995 | 142154600 | 0.0 | 0.0 |
| 3217 | 2023-04-11 00:00:00-04:00 | 186.690002 | 189.190002 | 185.649994 | 186.789993 | 115770900 | 0.0 | 0.0 |
| 3218 | 2023-04-12 00:00:00-04:00 | 190.740005 | 191.580002 | 180.309998 | 180.539993 | 150256300 | 0.0 | 0.0 |
| 3219 | 2023-04-13 00:00:00-04:00 | 182.960007 | 186.500000 | 180.940002 | 185.899994 | 112933000 | 0.0 | 0.0 |
| 3220 | 2023-04-14 00:00:00-04:00 | 183.949997 | 186.279999 | 182.009995 | 185.000000 | 96306500 | 0.0 | 0.0 |
gme = yf.Ticker("GME")
gme_data = gme.history(period="max")
gme_data.head()
| Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2002-02-13 00:00:00-05:00 | 1.620128 | 1.693350 | 1.603296 | 1.691666 | 76216000 | 0.0 | 0.0 |
| 2002-02-14 00:00:00-05:00 | 1.712708 | 1.716074 | 1.670627 | 1.683251 | 11021600 | 0.0 | 0.0 |
| 2002-02-15 00:00:00-05:00 | 1.683250 | 1.687458 | 1.658002 | 1.674834 | 8389600 | 0.0 | 0.0 |
| 2002-02-19 00:00:00-05:00 | 1.666418 | 1.666418 | 1.578047 | 1.607504 | 7410400 | 0.0 | 0.0 |
| 2002-02-20 00:00:00-05:00 | 1.615920 | 1.662210 | 1.603296 | 1.662210 | 6892800 | 0.0 | 0.0 |
import pandas as pd
import requests
from bs4 import BeautifulSoup
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html"
data = requests.get(url).text
soup = BeautifulSoup(data, 'html5lib')
gme_revenue = pd.DataFrame(columns=['Date', 'Revenue'])
for table in soup.find_all('table'):
if ('GameStop Quarterly Revenue' in table.find('th').text):
rows = table.find_all('tr')
for row in rows:
col = row.find_all('td')
if col != []:
date = col[0].text
revenue = col[1].text.replace(',','').replace('$','')
gme_revenue = gme_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
gme_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 57 | 2006-01-31 | 1667 |
| 58 | 2005-10-31 | 534 |
| 59 | 2005-07-31 | 416 |
| 60 | 2005-04-30 | 475 |
| 61 | 2005-01-31 | 709 |
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_2219/1765937996.py in <module> ----> 1 make_graph(tesla_data, tesla_revenue, "Tesla") NameError: name 'make_graph' is not defined
!pip install yfinance==0.1.67
#!pip install pandas==1.3.3
#!pip install requests==2.26.0
!mamba install bs4==4.10.0 -y
#!pip install plotly==5.3.1
!pip install html5lib
!pip install --upgrade nbformat
Collecting yfinance==0.1.67
Downloading yfinance-0.1.67-py2.py3-none-any.whl (25 kB)
Requirement already satisfied: pandas>=0.24 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (1.3.5)
Requirement already satisfied: requests>=2.20 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (2.28.1)
Requirement already satisfied: lxml>=4.5.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (4.9.2)
Requirement already satisfied: multitasking>=0.0.7 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (0.0.11)
Requirement already satisfied: numpy>=1.15 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (1.21.6)
Requirement already satisfied: python-dateutil>=2.7.3 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from pandas>=0.24->yfinance==0.1.67) (2.8.2)
Requirement already satisfied: pytz>=2017.3 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from pandas>=0.24->yfinance==0.1.67) (2022.6)
Requirement already satisfied: charset-normalizer<3,>=2 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.20->yfinance==0.1.67) (2.1.1)
Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.20->yfinance==0.1.67) (2022.12.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.20->yfinance==0.1.67) (1.26.13)
Requirement already satisfied: idna<4,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.20->yfinance==0.1.67) (3.4)
Requirement already satisfied: six>=1.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance==0.1.67) (1.16.0)
Installing collected packages: yfinance
Attempting uninstall: yfinance
Found existing installation: yfinance 0.2.4
Uninstalling yfinance-0.2.4:
Successfully uninstalled yfinance-0.2.4
Successfully installed yfinance-0.1.67
__ __ __ __
/ \ / \ / \ / \
/ \/ \/ \/ \
███████████████/ /██/ /██/ /██/ /████████████████████████
/ / \ / \ / \ / \ \____
/ / \_/ \_/ \_/ \ o \__,
/ _/ \_____/ `
|/
███╗ ███╗ █████╗ ███╗ ███╗██████╗ █████╗
████╗ ████║██╔══██╗████╗ ████║██╔══██╗██╔══██╗
██╔████╔██║███████║██╔████╔██║██████╔╝███████║
██║╚██╔╝██║██╔══██║██║╚██╔╝██║██╔══██╗██╔══██║
██║ ╚═╝ ██║██║ ██║██║ ╚═╝ ██║██████╔╝██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝
mamba (0.15.3) supported by @QuantStack
GitHub: https://github.com/mamba-org/mamba
Twitter: https://twitter.com/QuantStack
█████████████████████████████████████████████████████████████
Looking for: ['bs4==4.10.0']
pkgs/main/linux-64 [> ] (--:--) No change
pkgs/main/linux-64 [====================] (00m:00s) No change
pkgs/main/noarch [> ] (--:--) No change
pkgs/main/noarch [====================] (00m:00s) No change
pkgs/r/linux-64 [> ] (--:--) No change
pkgs/r/linux-64 [====================] (00m:00s) No change
pkgs/r/noarch [> ] (--:--) No change
pkgs/r/noarch [====================] (00m:00s) No change
Pinned packages:
- python 3.7.*
Transaction
Prefix: /home/jupyterlab/conda/envs/python
All requested packages already installed
Requirement already satisfied: html5lib in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (1.1)
Requirement already satisfied: webencodings in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from html5lib) (0.5.1)
Requirement already satisfied: six>=1.9 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from html5lib) (1.16.0)
Requirement already satisfied: nbformat in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (5.7.0)
Collecting nbformat
Downloading nbformat-5.8.0-py3-none-any.whl (77 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 77.4/77.4 kB 10.7 MB/s eta 0:00:00
Requirement already satisfied: jupyter-core in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat) (4.12.0)
Requirement already satisfied: traitlets>=5.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat) (5.6.0)
Requirement already satisfied: fastjsonschema in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat) (2.16.2)
Requirement already satisfied: jsonschema>=2.6 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat) (4.17.3)
Requirement already satisfied: importlib-metadata>=3.6 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat) (4.11.4)
Requirement already satisfied: typing-extensions>=3.6.4 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from importlib-metadata>=3.6->nbformat) (4.4.0)
Requirement already satisfied: zipp>=0.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from importlib-metadata>=3.6->nbformat) (3.11.0)
Requirement already satisfied: importlib-resources>=1.4.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema>=2.6->nbformat) (5.10.1)
Requirement already satisfied: attrs>=17.4.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema>=2.6->nbformat) (22.1.0)
Requirement already satisfied: pkgutil-resolve-name>=1.3.10 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema>=2.6->nbformat) (1.3.10)
Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema>=2.6->nbformat) (0.19.2)
Installing collected packages: nbformat
Attempting uninstall: nbformat
Found existing installation: nbformat 5.7.0
Uninstalling nbformat-5.7.0:
Successfully uninstalled nbformat-5.7.0
Successfully installed nbformat-5.8.0
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
tesla_revenue = pd.DataFrame(columns=['Date', 'Revenue'])
for table in soup.find_all('table'):
if ('Tesla Quarterly Revenue' in table.find('th').text):
rows = table.find_all('tr')
for row in rows:
col = row.find_all('td')
if col != []:
date = col[0].text
revenue = col[1].text.replace(',','').replace('$','')
tesla_revenue = tesla_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
make_graph(tesla_data, tesla_revenue, "Tesla")
gme_revenue = pd.DataFrame(columns = ['Date', 'Revenue'])
for row in soup.find_all("tbody")[1].find_all("tr"):
col = row.find_all("td")
Date = col[0].text
revenue = col[1].text.replace("$", "").replace(",", "")
gme_revenue = gme_revenue.append({"Date": Date, "Revenue": revenue}, ignore_index = True)
GameStop = yf.Ticker("GME")
gme_data = GameStop.history(period = 'max')
gme_data.reset_index(inplace = True)
gme_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 00:00:00-05:00 | 1.620128 | 1.693350 | 1.603296 | 1.691666 | 76216000 | 0.0 | 0.0 |
| 1 | 2002-02-14 00:00:00-05:00 | 1.712707 | 1.716073 | 1.670626 | 1.683250 | 11021600 | 0.0 | 0.0 |
| 2 | 2002-02-15 00:00:00-05:00 | 1.683250 | 1.687458 | 1.658001 | 1.674834 | 8389600 | 0.0 | 0.0 |
| 3 | 2002-02-19 00:00:00-05:00 | 1.666418 | 1.666418 | 1.578048 | 1.607505 | 7410400 | 0.0 | 0.0 |
| 4 | 2002-02-20 00:00:00-05:00 | 1.615920 | 1.662210 | 1.603296 | 1.662210 | 6892800 | 0.0 | 0.0 |
make_graph(gme_data, gme_revenue, 'GameStop')